/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package swing;

import config.MyConnection;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/**
 *
 * @author Dark
 */
public class PlayerLogin extends javax.swing.JFrame {

    ImageIcon showmes = new ImageIcon("/images/question_mark_icon_blueglow.png");
    ImageIcon showerror = new ImageIcon("images/dialog_warning.png");
    DefaultComboBoxModel cbbDayModel = new DefaultComboBoxModel();
    DefaultComboBoxModel cbbMonthModel = new DefaultComboBoxModel();
    DefaultComboBoxModel cbbYearModel = new DefaultComboBoxModel();
    Image icon = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/question_mark_icon_blueglow.png"));

    /**
     * Creates new form Player
     */
    public PlayerLogin() {
        initComponents();
        this.setIconImage(icon);
            cbbDayModel.addElement("Day");
            cbbMonthModel.addElement("Month");
            cbbYearModel.addElement("Year");
            setDay();
            setYear();
            setMonth();
            this.setLocationRelativeTo(null);
    }

    private void setDay() {
        for (int i = 1; i < 32; i++) {
            cbbDayModel.addElement(i);
        }
    }

    private void setMonth() {
        for (int i = 1; i <= 12; i++) {
            cbbMonthModel.addElement(i);
        }
    }

    private void setYear() {
        for (int i = 1945; i <= 2009; i++) {
            cbbYearModel.addElement(i);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnPlay = new javax.swing.JButton();
        txtName = new javax.swing.JTextField();
        txtAdd = new javax.swing.JTextField();
        txtEmail = new javax.swing.JTextField();
        imgLogo = new javax.swing.JLabel();
        imgTitle = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        lbName = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lbBottom = new javax.swing.JLabel();
        cbbMonth = new javax.swing.JComboBox();
        cbbDay = new javax.swing.JComboBox();
        cbbYear = new javax.swing.JComboBox();
        bgLogin = new javax.swing.JLabel();
        jMenuBar2 = new javax.swing.JMenuBar();
        mnSystem = new javax.swing.JMenu();
        mnConfig = new javax.swing.JMenuItem();
        mnExit = new javax.swing.JMenuItem();
        mnAdmin = new javax.swing.JMenu();
        mniAdmin = new javax.swing.JMenuItem();
        mnAbout = new javax.swing.JMenu();
        mniAbout = new javax.swing.JMenuItem();
        mnHelp = new javax.swing.JMenu();
        mniHelp = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("KBC Quiz Game");
        setMinimumSize(new java.awt.Dimension(1024, 700));
        setResizable(false);
        getContentPane().setLayout(null);

        btnPlay.setFont(new java.awt.Font("SimSun-ExtB", 1, 18));
        btnPlay.setForeground(new java.awt.Color(204, 204, 255));
        btnPlay.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/test.gif"))); // NOI18N
        btnPlay.setText("Play Now");
        btnPlay.setContentAreaFilled(false);
        btnPlay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPlayActionPerformed(evt);
            }
        });
        getContentPane().add(btnPlay);
        btnPlay.setBounds(560, 430, 150, 40);

        txtName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNameActionPerformed(evt);
            }
        });
        getContentPane().add(txtName);
        txtName.setBounds(550, 270, 230, 30);
        getContentPane().add(txtAdd);
        txtAdd.setBounds(550, 350, 230, 30);
        getContentPane().add(txtEmail);
        txtEmail.setBounds(550, 310, 230, 30);

        imgLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/20110210050148.png"))); // NOI18N
        getContentPane().add(imgLogo);
        imgLogo.setBounds(410, 20, 150, 180);

        imgTitle.setFont(new java.awt.Font("Segoe Script", 1, 48));
        imgTitle.setForeground(new java.awt.Color(102, 255, 255));
        imgTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        imgTitle.setText("KBC Quiz Game");
        getContentPane().add(imgTitle);
        imgTitle.setBounds(210, 60, 570, 110);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/money.png"))); // NOI18N
        getContentPane().add(jLabel1);
        jLabel1.setBounds(260, 270, 130, 160);

        lbName.setFont(new java.awt.Font("Tahoma", 0, 14));
        lbName.setForeground(new java.awt.Color(255, 255, 255));
        lbName.setText("Name");
        getContentPane().add(lbName);
        lbName.setBounds(440, 270, 50, 20);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Email");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(440, 320, 34, 17);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Address");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(440, 360, 60, 17);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Date Of Birth");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(440, 400, 100, 17);

        lbBottom.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbBottom.setForeground(new java.awt.Color(255, 255, 255));
        lbBottom.setText("Coppy Right © Group 4 -FPT-Aptech");
        getContentPane().add(lbBottom);
        lbBottom.setBounds(360, 620, 270, 20);

        cbbMonth.setModel(cbbMonthModel);
        cbbMonth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbbMonthActionPerformed(evt);
            }
        });
        getContentPane().add(cbbMonth);
        cbbMonth.setBounds(550, 390, 70, 30);

        cbbDay.setModel(cbbDayModel);
        getContentPane().add(cbbDay);
        cbbDay.setBounds(630, 390, 70, 30);

        cbbYear.setModel(cbbYearModel);
        getContentPane().add(cbbYear);
        cbbYear.setBounds(710, 390, 70, 30);

        bgLogin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/TestLoginBackground.png"))); // NOI18N
        bgLogin.setFocusable(false);
        getContentPane().add(bgLogin);
        bgLogin.setBounds(0, 0, 1024, 700);

        mnSystem.setText("System");

        mnConfig.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/config.png"))); // NOI18N
        mnConfig.setText("Config");
        mnConfig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnConfigActionPerformed(evt);
            }
        });
        mnSystem.add(mnConfig);

        mnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit.png"))); // NOI18N
        mnExit.setText("Exit");
        mnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnExitActionPerformed(evt);
            }
        });
        mnSystem.add(mnExit);

        jMenuBar2.add(mnSystem);

        mnAdmin.setText("Admin");

        mniAdmin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/administrator.png"))); // NOI18N
        mniAdmin.setText("Admin Cpanel");
        mniAdmin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniAdminActionPerformed(evt);
            }
        });
        mnAdmin.add(mniAdmin);

        jMenuBar2.add(mnAdmin);

        mnAbout.setText("About");
        mnAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnAboutActionPerformed(evt);
            }
        });

        mniAbout.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/about.png"))); // NOI18N
        mniAbout.setText("About");
        mniAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniAboutActionPerformed(evt);
            }
        });
        mnAbout.add(mniAbout);

        jMenuBar2.add(mnAbout);

        mnHelp.setText("Help");

        mniHelp.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        mniHelp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/help_copy.png"))); // NOI18N
        mniHelp.setText("Help");
        mniHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniHelpActionPerformed(evt);
            }
        });
        mnHelp.add(mniHelp);

        jMenuBar2.add(mnHelp);

        setJMenuBar(jMenuBar2);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mniAdminActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniAdminActionPerformed
        AdminLogin am = new AdminLogin();
        am.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_mniAdminActionPerformed

    private void mnConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnConfigActionPerformed
        new Config(null, true).setVisible(true);
    }//GEN-LAST:event_mnConfigActionPerformed

    private void txtNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNameActionPerformed

    private void btnPlayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPlayActionPerformed
        int D = 0;
        int M = 0;
        int Y = 0;
        int cbDay = cbbDay.getSelectedIndex();
        if (cbDay != 0) {
            D = Integer.parseInt(cbbDay.getSelectedItem().toString());
        }
        int cbMonth = cbbMonth.getSelectedIndex();
        if (cbMonth != 0) {
            M = Integer.parseInt(cbbMonth.getSelectedItem().toString());
        }
        int cbYear = cbbYear.getSelectedIndex();
        if (cbYear != 0) {
            Y = Integer.parseInt(cbbYear.getSelectedItem().toString());
        }

        String mail = ".+@.+\\.[a-z]+";
        Pattern p = Pattern.compile(mail);
        Matcher m = p.matcher(txtEmail.getText());
        if (txtName.getText().isEmpty()) {
            new ShowMessageDialog(null, true, "Please enter name", "Confirmation", ShowMessageDialog.MESSAGE);
            txtName.requestFocus();
            return;
        }
        if (txtEmail.getText().isEmpty()) {
            new ShowMessageDialog(null, true, "Please Enter Email", "Confirmation", ShowMessageDialog.MESSAGE);
            txtEmail.requestFocus();
            return;
        }
        if (!(m.matches())) {
            new ShowMessageDialog(null, true, "Please Check Email", "Confirmation", ShowMessageDialog.MESSAGE);
            txtEmail.requestFocus();
            return;
        }
        if (txtAdd.getText().isEmpty()) {
            new ShowMessageDialog(null, true, "Please enter address", "Confirmation", ShowMessageDialog.MESSAGE);
            txtAdd.requestFocus();
            return;
        }
        if (cbbMonth.getSelectedIndex() == 0) {
            new ShowMessageDialog(null, true, "Please choice Month", "Confirmation", ShowMessageDialog.MESSAGE);
            return;
        }
        if (cbbDay.getSelectedIndex() == 0) {
            new ShowMessageDialog(null, true, "Please choice Day", "Confirmation", ShowMessageDialog.MESSAGE);
            return;
        }
        if (cbbYear.getSelectedIndex() == 0) {
            new ShowMessageDialog(null, true, "Please choice Year", "Confirmation", ShowMessageDialog.MESSAGE);
            return;
        }
        if ((M == 4 || M == 6 || M == 9 || M == 11) && (D == 31)) {
            JOptionPane.showMessageDialog(rootPane, "Month " + cbbMonth.getSelectedItem() + " Only 30 Day", "KBC Quiz Show", JOptionPane.ERROR_MESSAGE, showerror);
            cbbDay.requestFocus();
            return;
        }
        if (((Y % 4 == 0 && Y % 100 != 0) || (Y % 400 == 0)) && (M == 2 && D >= 29)) {
            new ShowMessageDialog(null, true, "Month 2 of the leap year only 28 days", "Confirmation", ShowMessageDialog.MESSAGE);
            cbbDay.requestFocus();
            return;
        }
        if (M == 2 && D >= 30) {
            new ShowMessageDialog(null, true, "Month 2 Only 29 Day", "Confirmation", ShowMessageDialog.MESSAGE);
            cbbDay.requestFocus();
            return;
        }
        ConfigVariable.Username = txtName.getText();
        ConfigVariable.Email = txtEmail.getText();
        ConfigVariable.Address = txtAdd.getText();
        //get day, month, year
        Object day = cbbDay.getSelectedItem();
        Object month = cbbMonth.getSelectedItem();
        Object year = cbbYear.getSelectedItem();
        //get DateofDay
        Object birthDay = day + "/" + month + "/" + year;
        ConfigVariable.DateOfBirth = birthDay;
        new RuleOfGame(null, true).setVisible(true);
        this.dispose();
    }//GEN-LAST:event_btnPlayActionPerformed

    private void mnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnExitActionPerformed
        System.exit(0);
    }//GEN-LAST:event_mnExitActionPerformed

    private void mnAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnAboutActionPerformed
    }//GEN-LAST:event_mnAboutActionPerformed

    private void mniAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniAboutActionPerformed
        new About(null, true).setVisible(true);
    }//GEN-LAST:event_mniAboutActionPerformed

private void cbbMonthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbbMonthActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_cbbMonthActionPerformed

    private void mniHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniHelpActionPerformed
        try {
            String path = new File("").getAbsolutePath() + "\\Help.chm";
            File file = new File(path);

            if (file.exists()) {
                Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler " + path);
            } else {
                throw new Exception("File \"Help.chm\" not found!");
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage(), "Notice", JOptionPane.OK_OPTION);
        }

    }//GEN-LAST:event_mniHelpActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PlayerLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PlayerLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PlayerLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PlayerLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        /*
         * Create and display the form
         */
        try {

            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                config.Config.loadConfig();
                if (!MyConnection.connect(config.Config.getProperty(config.Config.CONFIG_SERVERNAME),
                        config.Config.getProperty(config.Config.CONFIG_PORTNUMBER),
                        config.Config.getProperty(config.Config.CONFIG_DATABASENAME),
                        config.Config.getProperty(config.Config.CONFIG_SERVERUSERNAME),
                        config.Config.getProperty(config.Config.CONFIG_SERVERPASSWORD))) {
                    new ShowMessageDialog(null, true, "Can not connect to server", "Confirmation", ShowMessageDialog.MESSAGE);
                    swing.Config cf = new swing.Config(null, true);
                    cf.setVisible(true);
                    new PlayGame().setVisible(false);
                } else {
                    new PlayerLogin().setVisible(true);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel bgLogin;
    private javax.swing.JButton btnPlay;
    private javax.swing.JComboBox cbbDay;
    private javax.swing.JComboBox cbbMonth;
    private javax.swing.JComboBox cbbYear;
    private javax.swing.JLabel imgLogo;
    private javax.swing.JLabel imgTitle;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JLabel lbBottom;
    private javax.swing.JLabel lbName;
    private javax.swing.JMenu mnAbout;
    private javax.swing.JMenu mnAdmin;
    private javax.swing.JMenuItem mnConfig;
    private javax.swing.JMenuItem mnExit;
    private javax.swing.JMenu mnHelp;
    private javax.swing.JMenu mnSystem;
    private javax.swing.JMenuItem mniAbout;
    private javax.swing.JMenuItem mniAdmin;
    private javax.swing.JMenuItem mniHelp;
    private javax.swing.JTextField txtAdd;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtName;
    // End of variables declaration//GEN-END:variables
}
